package com.campus.dao.impl;

import com.campus.dao.StudentDao;
import com.campus.model.Student;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @author HLQ
 * @date 2024/11/17 22:20
 */
@Slf4j
@Repository
public class StudentDaoImpl implements StudentDao {
    @Autowired
    JdbcTemplate jdbcTemplate;

    private final RowMapper<Student> studentRowMapper = new RowMapper<Student>() {
        @Override
        public Student mapRow(ResultSet rs, int rowNum) throws SQLException {
            Student student = new Student()
                    .setId(rs.getInt("id"))
                    .setStuName(rs.getString("stu_name"))
                    .setFromPlace(rs.getString("from_place"))
                    .setParentPhone(rs.getString("parent_phone"))
                    .setCurrentGrade(rs.getString("current_grade"))
                    .setIsFirst(rs.getString("is_first"))
                    .setLoginId(rs.getInt("login_id"))
                    .setComment(rs.getString("comment"));
            return student;
        }
    };

    @Override
    public int addStudent(Student student) {
        String sql = "INSERT INTO t_manager_student " +
                "(stu_name, from_place, parent_phone, current_grade, is_first,login_id) " +
                "VALUES (?,?,?,?,?,?)";
        Object[] args = new Object[]{
                student.getStuName(),
                student.getFromPlace(),
                student.getParentPhone(),
                student.getCurrentGrade(),
                student.getIsFirst(),
                student.getLoginId()
        };
        return jdbcTemplate.update(sql, args);
    }

    @Override
    public int deleteStudentById(int id) {
        String sql = "DELETE FROM t_manager_student WHERE id = ?";
        return jdbcTemplate.update(sql, id);
    }

    @Override
    public int updateStudentById(Student student) {
        String sql = "UPDATE t_manager_student SET " +
                "stu_name = ?, from_place = ?, parent_phone = ?, current_grade = ?, is_first = ?, login_id = ? WHERE id = ?";
        Object[] args = new Object[]{
                student.getStuName(),
                student.getFromPlace(),
                student.getParentPhone(),
                student.getCurrentGrade(),
                student.getIsFirst(),
                student.getLoginId(),
                student.getId()
        };
        return jdbcTemplate.update(sql, args);
    }

    @Override
    public Student queryStudentById(int id) {
        String sql = "SELECT * FROM t_manager_student WHERE id = ?";
        return jdbcTemplate.queryForObject(sql, new Object[]{id}, studentRowMapper);
    }

    @Override
    public int checkStudentById(String comment,int id) {
        String sql = "UPDATE t_manager_student SET comment = ? WHERE id = ?";
        return jdbcTemplate.update(sql, new Object[]{comment, id});
    }

}
